<template>
	<view class="content-1-box">
		<view class="mt-30 font-24">
			<view class="title-box mr-22 font-28">取消原因: </view>
			<view class="mb-24">
				<u-tag v-for="item in evaluateTemplateData" :key="item" class="mr-16 mt-24" :text="item" mode="dark"
					bg-color="#eeeeee" color="#666" @click="addEvaluateTemplate(item)"
					:class="{'is-current' : evaluateTemplate.some(items => items == item)}" />
			</view>
			<u-input v-model="reason" type="textarea" :border="false" placeholder="告诉商家,激励我们做得更好" />
			<view class="mt-24">
				<u-upload :action="action" :max-count="1" :header="header" :file-list="fileList" @on-remove="onRemove"
					@on-success="onSuccess"></u-upload>
			</view>
		</view>
		<view class="mx-32 py-60">
			<u-button type="warning" :custom-style="customStyle" @click="submit()">提交理由</u-button>
		</view>
	</view>
</template>

<script>
	import {
		baseUrl
	} from '@/common/request/baseurl.js';
	import {
		mapState
	} from 'vuex';
	export default {
		data() {
			return {
				customStyle: {
					backgroundColor: '#f0ad4e'
				},
				action: baseUrl + '/comFileDetail/uploadPublicFile',
				header: {
					publictoken: uni.getStorageSync('clinetToken')
				},
				evaluateTemplateData: [], // 评价模板
				evaluateTemplate: [], // 评价模板结果
				reason: '',
				image: '',
				fileList: [],
				submitInfo: {},
				index: 0
			}
		},
		onLoad(e) {
			this.submitInfo = {
				orderTakeawayId: e.orderTakeawayId,
				shopId: e.shopId,
			};
			this.index = e.index;
			this.getOrderTakeawayCancellationTemplate();
		},
		computed: {
			...mapState(['userInfo'])
		},
		methods: {
			// 提交数据
			submit() {
				if (this.evaluateTemplate.length == 0) {
					uni.showToast({
						title: '取消原因必选',
						image: "/static/icon/error.png"
					})
					return false;
				}
				this.$u.api.userApplyOrderTakeawayCancellation({
					...this.submitInfo,
					userId: this.userInfo.id,
					reason: this.reason,
					image: this.image,
					template: this.evaluateTemplate.join(',')
				}).then(res => {
					uni.showToast({
						title: '申请成功，等待平台审核'
					})
					this.$api.prePage().orderList[this.index].payState = 3;
					setTimeout(() => {
						uni.navigateBack();
					}, 800)
				})
			},
			// 上传图片
			onSuccess(data, index, lists, name) {
				this.image = data.data;
			},
			// 删除图片
			onRemove(data) {
				this.image = ''
			},
			// 添加评价模板信息
			addEvaluateTemplate(item) {
				// 查找数组是否存在改值
				const indexs = this.evaluateTemplate.findIndex((items) => items == item);
				if (indexs == -1) {
					// 不存在就添加
					this.evaluateTemplate.push(item);
				} else {
					// 存在就删除
					this.evaluateTemplate.splice(this.evaluateTemplate.indexOf(item), 1)
				}
			},
			// 获取系统参数
			async getOrderTakeawayCancellationTemplate() {
				const data = await this.$u.api.getOrderTakeawayCancellationTemplate();
				this.evaluateTemplateData = data.data.split(',');
				console.log(this.evaluateTemplateData);
			}
		}
	}
</script>

<style lang="scss" scoped>
	page {
		background: #fff !important;
	}

	.title-box {
		position: relative;
		padding-left: 12rpx;

		&::before {
			content: '*';
			position: absolute;
			top: 12rpx;
			left: 0;
			font-size: 24rpx;
			color: #fd3a4c;
		}
	}

	.content-1-box {
		background-color: #fff;
		border-radius: 16rpx;
		// padding: 30rpx;
		margin: 30rpx;

		.line {
			border-bottom: 1rpx solid #eaeaea;
			opacity: .3;
		}
	}

	.textarea_box {
		background-color: #f8f8f8;
	}

	.satisfied-box {
		background-color: #f8f8f8;
		padding: 8rpx 60rpx;
		border-radius: 16rpx;
	}

	.active {
		background-color: $uni-color-warning;
		color: #fff;
	}

	.is-current {
		background-color: $uni-color-warning !important;
		color: #ff007f !important;
	}

	::v-deep {
		.u-input__textarea {
			background-color: #f8f8f8;
			padding-left: 12rpx;
			padding-right: 12rpx;
			border-radius: 8rpx;
		}

		.uni-textarea-placeholder {
			color: #999 !important;
		}

		.u-star-wrap {
			margin-right: 12rpx;
		}
	}

	page {
		background: #f8f8f8;
	}

	.divider-box {
		margin: 0 120rpx;
	}
</style>